$(function () {
    $("#jqGrid").Grid({
        url: '../fs/fireextinguisher/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '灭火器名称', name: 'name', index: 'name', width: 80},
			{label: '购买公司', name: 'linkClientEnterprise', index: 'link_client_enterprise', width: 80,hidden: true},
			{label: '企业名称', name: 'clientEnterpriseName', index: 'client_enterprise_name', width: 80},
			{label: '状态', name: 'status', index: 'status', width: 80},
			{label: '出售时间', name: 'createTime', index: 'create_time', width: 80},
			// {label: 'createUserId', name: 'createUserId', index: 'create_user_id', width: 80,hidden: true},
			// {label: 'createUserName', name: 'createUserName', index: 'create_user_name', width: 80,hidden: true},
			{label: '有效期', name: 'period', index: 'period', width: 80},
			// {label: '二维码', name: 'qrCodeUrl', index: 'qr_code_url', width: 80},
			// {label: '二维码', name: 'qrCode', index: 'qr_code', width: 80},
			{label: '位置', name: 'position', index: 'position', width: 80},
			{label: '经度', name: 'longitude', index: 'longitude', width: 80},
			{label: '纬度', name: 'latitude', index: 'latitude', width: 80}]
    });
});

let vm = new Vue({
	el: '#rrapp',
	data: {
		longitude: '',
		latitude: '',
		position: '',
        showList: true,
        title: null,
		fireExtinguisher: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: ''
		},
		cityList: [
			{
				address: "福州",
				createTime: null,
				createUser: null,
				id: "5",
				latitude: null,
				longitude: null,
				name: "博思软件",
				position: null,
			},
		],
		statusList: [
			{
				value: '1',
				label: '正常'
			},
			{
				value: '0',
				label: '异常'
			}
		],
		model11: '',
		model12: [],
		//定位相关变量
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.fireExtinguisher = {};
		},
		update: function (event) {
            let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            let url = vm.fireExtinguisher.id == null ? "../fs/fireextinguisher/save" : "../fs/fireextinguisher/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.fireExtinguisher),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../fs/fireextinguisher/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../fs/fireextinguisher/info/"+id,
                async: true,
                successCallback: function (r) {
					vm.fireExtinguisher = r.fireExtinguisher;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                name: '',
				clientEnterpriseName: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
		detail: function () {

		},
		getEnterprises: function (event) {
			that = this;
			console.log(this.cityList)
			Ajax.request({
				url: '../fs/cliententerprise/queryAll',
				params: JSON.stringify(''),
				type: "GET",
				contentType: "application/json",
				successCallback: function (r) {
					debugger
					that.cityList = r.list;
				}
			});
		},
		initDatas(){
			window.addEventListener('message', function(event) {
				// 接收位置信息，用户选择确认位置点后选点组件会触发该事件，回传用户的位置信息
				var loc = event.data;
				if (loc && loc.module == 'locationPicker') {//防止其他应用也会向该页面post信息，需判断module是否为'locationPicker'
					console.log('location', loc);
					//经度
					vm.fireExtinguisher.longitude = loc.latlng.lng;
					//纬度
					vm.fireExtinguisher.latitude = loc.latlng.lat;
					vm.fireExtinguisher.position = loc.poiaddress;
				}
			}, false);
		}
	},
	mounted: function () {
		this.initDatas();
		this.getEnterprises();
	}
});
